package com.net.requesthandler
{
	import com.net.Event.HttpEvent;
	import com.net.http.HttpRequest;
	import com.net.http.HttpStateCode;
	import com.net.http.RequestHeader;
	import com.net.http.RequestStream;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	

	public class uploadRequsetHandler implements IRequestHandler
	{
		private var request_header:RequestHeader;
		
		private var requeststream:RequestStream;
		
		private var request:HttpRequest;
		
		private var dataLegth:uint;//post数据长度
		
		private var content_type:String;//主体数据类型
		
		private var boundaryBytes:ByteArray;
		
		private var filestream:FileStream;//要写入的文件
		
		private var upload:upLoadFile;//用于分析上传的数据段
		
		private var bytes:ByteArray;//上传数据的缓冲
		
		
		public function uploadRequsetHandler()
		{
			bytes=new ByteArray();
		}
		
		public function setRequest(client:HttpRequest):void{
			request=client;
			request_header=client.getRequestHeader();
			requeststream=client.getRequestStream();
			
			
			
			if(request_header.getRequestMode()=="POST"){
				dataLegth=uint(request_header.getParmeter("Content-Length"));//获取主体数据长度
				
				content_type=request_header.getParmeter("Content-Type");//获取post数据的格式
				
				var boundary:String=content_type.substring(content_type.indexOf("boundary=")+9,content_type.length);//获取分割线定义
				boundaryBytes=new ByteArray();
				boundaryBytes.writeUTFBytes(boundary);
				
				requeststream.addEventListener(HttpEvent.POST_ARRIVE,on_data);
			}else{
				
				request_header.setReponseHeader(HttpStateCode.BAD_REQUEST);
				requeststream.writeReponseHeader(request_header);
				request.close();
			}
		}
		
		//数据到达
		private function on_data(event:HttpEvent):void{
			var temp:ByteArray=new ByteArray();
			requeststream.readBytes(temp);
			bytes.writeBytes(temp);
			
			if(bytes.length==dataLegth){
				//数据读取完成
				createFile();
			}
		}
		
		/**
		 * 在数据中查找文件
		 */
		private function createFile():void{
			var start:int=0;
			while(start>-1){
				var end:int=findFile(start+2+boundaryBytes.length+2);//start+"--"+"boundary"+"\r\n"
				if(end>-1){
					//var temp:ByteArray=new ByteArray();
					//temp.writeBytes(bytes,start,end-start);
					bytes.position=start;
					var file:upLoadFile=new upLoadFile(bytes,end-start);
					start=end;
				}else{
					request.close();
					break;
				}
			}
		}
		
		
		/**
		 * 开始搜索文件
		 * 如果找到Boundary所在行则返回所在行第一个引用  为两个--
		 * 找不到则返回-1
		 */
		private function findFile(startAt:int=0):int{
			var out:int=-1;
			var slength:int=bytes.length;
			
			//查找 --
			for (var m:int=startAt;m<slength;m++ ){
				if (bytes[m] == 45  && bytes[m+1] == 45) {
					
					//对比Boundary
					var boolean:Boolean=true;
					for(var k:int=0;k<boundaryBytes.length;k++){
						if(bytes[m+2+k]!=boundaryBytes[k]){
							boolean=false;
						}
					}
					if(boolean){
						out=m;
						break;
					}
				}
			}
			return out
		}
	}
}